//
//  AppDelegate.m
//  JXAddressApp
//
//  Created by zhhw_home on 2018/10/25.
//  Copyright © 2018年 zhhw_home. All rights reserved.
//

#import "AppDelegate.h"
#import "JXTabbarVC.h"
#import "QMUINavigationController.h"
#import "BNCoreServices.h"
#import "EgovaNetworking.h"
#import <Bugly/Bugly.h>
#import <QMUIKit/UIImage+QMUI.h>
#import <AFNetworking/AFNetworkReachabilityManager.h>
#import "NSDictionary+JSON.h"

#define BAIDU_KEY @"VHlZ5H38IfqvL7cy7WzoGZWN4zIHvD8t"

#define APP_ID @"15165324"
#define ApiKey @"VHlZ5H38IfqvL7cy7WzoGZWN4zIHvD8t"
#define SecretKey @"ZVR4QXhdHpei7SDQDh65hfDKvnF7VNE2"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [Bugly startWithAppId:@"160a7d72a0"];
    if(@available(iOS 11.0, *)) {
        [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    [EgovaNetworking configRequestType:kEgovaRequestTypePlainText responseType:kEgovaResponseTypeJSON shouldAutoEncodeUrl:YES callbackOnCancelRequest:NO];
    
    JXTabbarVC *tabVC = [[JXTabbarVC alloc] init];
//    QMUINavigationController *navigationVC = [[QMUINavigationController alloc] initWithRootViewController:tabVC];
//
    UIImage *image = [UIImage qmui_imageWithColor:[UIColor whiteColor]];
//
    [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = tabVC;
    
    NSString * str = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.wegis.net/demo/data/mobilemenu.json"] encoding:NSUTF8StringEncoding error:nil];
    NSDictionary *JSON = [self readInterfaceValue:str];
    

        if (JSON) {
            NSNumber *type = JSON[@"mapType"];
            
            if (type) {
                [tabVC initTabbar:[type integerValue]];
            }
            else {
                [tabVC initTabbar:2];
            }
        }
    
        [self.window makeKeyAndVisible];
    
    [BNCoreServices_Instance startServicesAsyn:^{
        [BNCoreServices_Instance authorizeNaviAppKey:BAIDU_KEY completion:^(BOOL suc) {
            NSLog(@"authorizeNaviAppKey %d:", suc);
        }];
        
        [BNCoreServices_Instance authorizeTTSAppId:APP_ID apiKey:ApiKey secretKey:SecretKey completion:^(BOOL suc) {
            NSLog(@"authorizeTTSAppId %d:", suc);
        }];
        
    } fail:^{
        
    }];
    
    return YES;
}

- (NSDictionary *)readInterfaceValue:(NSString*)allStr
{
    NSScanner *myScanner = [NSScanner scannerWithString:allStr];//扫描
    NSString *annotStr = nil;
    NSString *jsonStr = allStr;
    while ([myScanner isAtEnd] == NO) {
        //开始扫描
        [myScanner scanUpToString:@"//" intoString:NULL];
        [myScanner scanUpToString:@"\r\n" intoString:&annotStr];
        //将结果替换
        //注意 这样写jsonStr =  [jsonStr stringByReplacingOccurrencesOfString:annotStr withString:@""]; 无法区分json中的”// 事项“和”// 事项备注“ 两个注释
        jsonStr = [jsonStr stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@\n", annotStr] withString:@"\n"];
    }
    if (jsonStr == nil) {return nil;}
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if(error) {
        NSLog(@"json解析失败：%@",error);
        return nil;
    }
    return resultDic;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
